package com.astersoft.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

import com.astersoft.lib.cypher.ASCrypt;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import com.astersoft.lib.cypher.ASCrypt.CyAlgorithm;
import com.astersoft.lib.text.ASURLUTF8Utils;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class ASLibGui
extends JFrame {
	private static final long serialVersionUID = 1L;
	private JTextField txtInput;
	private JTextArea txtOuput;
	private JComboBox comboBox;
	private JTextArea codedArea;
	private JTextArea decodedArea;

	public ASLibGui() {
		setLocationByPlatform(true);
		setPreferredSize(new Dimension(500, 350));
		setSize(new Dimension(500, 350));

		initGUI();

		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}

	private void initGUI() {
		setTitle("ASLib Gui Utils v1.0");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		getContentPane().add(tabbedPane, BorderLayout.CENTER);

		JPanel encryptPane = new JPanel();
		tabbedPane.addTab("Encryption Tools", null, encryptPane, null);
		GridBagLayout gbl_encryptPane = new GridBagLayout();
		gbl_encryptPane.columnWidths = new int[] { 103, 0, 0, 0 };
		gbl_encryptPane.rowHeights = new int[] { 0, 0, 100, 0, 0 };
		gbl_encryptPane.columnWeights = new double[] { 0.0, 1.0, 0.0, Double.MIN_VALUE };
		gbl_encryptPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		encryptPane.setLayout(gbl_encryptPane);

		JLabel lblTitle = new JLabel(
		"<html><center>Da questa schermata \u00E8 possibile testare le funzionalit\u00E0 <br/>\r\ndi crypt implementate in ASLib 1.0</center></html>");
		lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblTitle.setForeground(new Color(30, 144, 255));
		lblTitle.setFont(new Font("Tahoma", Font.BOLD, 15));
		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.fill = GridBagConstraints.BOTH;
		gbc_lblTitle.gridwidth = 3;
		gbc_lblTitle.insets = new Insets(0, 0, 5, 0);
		gbc_lblTitle.gridx = 0;
		gbc_lblTitle.gridy = 0;
		encryptPane.add(lblTitle, gbc_lblTitle);

		JLabel lblInserisciStringa = new JLabel("Inserisci stringa:");
		GridBagConstraints gbc_lblInserisciStringa = new GridBagConstraints();
		gbc_lblInserisciStringa.insets = new Insets(0, 0, 5, 5);
		gbc_lblInserisciStringa.anchor = GridBagConstraints.EAST;
		gbc_lblInserisciStringa.gridx = 0;
		gbc_lblInserisciStringa.gridy = 1;
		encryptPane.add(lblInserisciStringa, gbc_lblInserisciStringa);

		txtInput = new JTextField();
		GridBagConstraints gbc_txtInput = new GridBagConstraints();
		gbc_txtInput.fill = GridBagConstraints.BOTH;
		gbc_txtInput.insets = new Insets(0, 0, 5, 5);
		gbc_txtInput.gridx = 1;
		gbc_txtInput.gridy = 1;
		encryptPane.add(txtInput, gbc_txtInput);
		txtInput.setColumns(10);

		JButton btnEncrypt = new JButton("Encrypt");
		btnEncrypt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					txtOuput.setText(ASCrypt.encrypt(txtInput.getText(), (CyAlgorithm) comboBox.getSelectedItem()));
				} catch (Exception e1) {
					txtOuput.setText("##ERROR## " + e1.getMessage());
				}
			}
		});
		GridBagConstraints gbc_btnEncrypt = new GridBagConstraints();
		gbc_btnEncrypt.insets = new Insets(0, 0, 5, 0);
		gbc_btnEncrypt.fill = GridBagConstraints.BOTH;
		gbc_btnEncrypt.gridx = 2;
		gbc_btnEncrypt.gridy = 1;
		encryptPane.add(btnEncrypt, gbc_btnEncrypt);

		JLabel lblNewLabel = new JLabel("Output:");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 2;
		encryptPane.add(lblNewLabel, gbc_lblNewLabel);

		final JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 2;
		encryptPane.add(scrollPane, gbc_scrollPane);

		txtOuput = new JTextArea();
		this.txtOuput.setEditable(false);
		this.txtOuput.setLineWrap(true);
		scrollPane.setViewportView(this.txtOuput);
		txtOuput.setColumns(10);

		JButton btnDecrypt = new JButton("Decrypt");
		btnDecrypt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					txtOuput.setText(ASCrypt.decrypt(txtInput.getText(), (CyAlgorithm) comboBox.getSelectedItem()));
				} catch (Exception e1) {
					txtOuput.setText("##ERROR## " + e1.getMessage());
				}
			}
		});
		GridBagConstraints gbc_btnDecrypt = new GridBagConstraints();
		gbc_btnDecrypt.anchor = GridBagConstraints.NORTH;
		gbc_btnDecrypt.insets = new Insets(0, 0, 5, 0);
		gbc_btnDecrypt.gridx = 2;
		gbc_btnDecrypt.gridy = 2;
		encryptPane.add(btnDecrypt, gbc_btnDecrypt);

		final JLabel lblAlgoritmo = new JLabel("Algoritmo:");
		GridBagConstraints gbc_lblAlgoritmo = new GridBagConstraints();
		gbc_lblAlgoritmo.insets = new Insets(0, 0, 0, 5);
		gbc_lblAlgoritmo.anchor = GridBagConstraints.EAST;
		gbc_lblAlgoritmo.gridx = 0;
		gbc_lblAlgoritmo.gridy = 3;
		encryptPane.add(lblAlgoritmo, gbc_lblAlgoritmo);

		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(CyAlgorithm.values()));
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 0, 5);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 1;
		gbc_comboBox.gridy = 3;
		encryptPane.add(comboBox, gbc_comboBox);

		JPanel urlPanel = new JPanel();
		tabbedPane.addTab("URL Encode/Decode", null, urlPanel, null);
		GridBagLayout gbl_urlPanel = new GridBagLayout();
		gbl_urlPanel.columnWidths = new int[] { 5, 0, 5, 0 };
		gbl_urlPanel.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0 };
		gbl_urlPanel.columnWeights = new double[] { 0.0, 1.0, 0.0, Double.MIN_VALUE };
		gbl_urlPanel.rowWeights = new double[] { 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		urlPanel.setLayout(gbl_urlPanel);

		JLabel lbldaQuestaSchermata = new JLabel(
		"<html><center>Da questa schermata \u00E8 possibile testare le funzionalit\u00E0 <br/>\r\ndi URL UTF-8 Coding/Decoding di ASLib 1.0</center></html>");
		lbldaQuestaSchermata.setHorizontalAlignment(SwingConstants.CENTER);
		lbldaQuestaSchermata.setForeground(new Color(30, 144, 255));
		lbldaQuestaSchermata.setFont(new Font("Tahoma", Font.BOLD, 15));
		GridBagConstraints gbc_lbldaQuestaSchermata = new GridBagConstraints();
		gbc_lbldaQuestaSchermata.insets = new Insets(0, 0, 5, 5);
		gbc_lbldaQuestaSchermata.gridx = 1;
		gbc_lbldaQuestaSchermata.gridy = 0;
		urlPanel.add(lbldaQuestaSchermata, gbc_lbldaQuestaSchermata);

		JLabel lblUrldaCodificare = new JLabel("URL da codificare:");
		lblUrldaCodificare.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_lblUrldaCodificare = new GridBagConstraints();
		gbc_lblUrldaCodificare.anchor = GridBagConstraints.WEST;
		gbc_lblUrldaCodificare.insets = new Insets(0, 0, 5, 5);
		gbc_lblUrldaCodificare.gridx = 1;
		gbc_lblUrldaCodificare.gridy = 1;
		urlPanel.add(lblUrldaCodificare, gbc_lblUrldaCodificare);

		JScrollPane scrollPane_1 = new JScrollPane();
		GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
		gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_1.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane_1.gridx = 1;
		gbc_scrollPane_1.gridy = 2;
		urlPanel.add(scrollPane_1, gbc_scrollPane_1);

		final JTextArea sourceArea = new JTextArea();
		sourceArea.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				String input_text = sourceArea.getText();
				String encoded_text = ASURLUTF8Utils.encode(input_text);
				codedArea.setText(encoded_text);
				decodedArea.setText(ASURLUTF8Utils.decode(encoded_text));
			}
		});
		sourceArea.setLineWrap(true);
		scrollPane_1.setViewportView(sourceArea);

		JLabel lblUrlCodificato = new JLabel("URL Codificato:");
		GridBagConstraints gbc_lblUrlCodificato = new GridBagConstraints();
		gbc_lblUrlCodificato.insets = new Insets(0, 0, 5, 5);
		gbc_lblUrlCodificato.anchor = GridBagConstraints.WEST;
		gbc_lblUrlCodificato.fill = GridBagConstraints.VERTICAL;
		gbc_lblUrlCodificato.gridx = 1;
		gbc_lblUrlCodificato.gridy = 3;
		urlPanel.add(lblUrlCodificato, gbc_lblUrlCodificato);

		JScrollPane scrollPane_2 = new JScrollPane();
		GridBagConstraints gbc_scrollPane_2 = new GridBagConstraints();
		gbc_scrollPane_2.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane_2.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_2.gridx = 1;
		gbc_scrollPane_2.gridy = 4;
		urlPanel.add(scrollPane_2, gbc_scrollPane_2);

		codedArea = new JTextArea();
		this.codedArea.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				String input_text = codedArea.getText();
				decodedArea.setText(ASURLUTF8Utils.decode(input_text));
			}
		});
		codedArea.setLineWrap(true);
		scrollPane_2.setViewportView(codedArea);

		JLabel lblUrlDecodificato = new JLabel("URL Decodificato:");
		GridBagConstraints gbc_lblUrlDecodificato = new GridBagConstraints();
		gbc_lblUrlDecodificato.fill = GridBagConstraints.BOTH;
		gbc_lblUrlDecodificato.insets = new Insets(0, 0, 5, 5);
		gbc_lblUrlDecodificato.gridx = 1;
		gbc_lblUrlDecodificato.gridy = 5;
		urlPanel.add(lblUrlDecodificato, gbc_lblUrlDecodificato);

		JScrollPane scrollPane_3 = new JScrollPane();
		GridBagConstraints gbc_scrollPane_3 = new GridBagConstraints();
		gbc_scrollPane_3.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane_3.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_3.gridx = 1;
		gbc_scrollPane_3.gridy = 6;
		urlPanel.add(scrollPane_3, gbc_scrollPane_3);

		decodedArea = new JTextArea();
		decodedArea.setLineWrap(true);
		decodedArea.setEditable(false);
		scrollPane_3.setViewportView(decodedArea);

		final JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				codedArea.setText("");
				sourceArea.setText("");
				decodedArea.setText("");
			}
		});
		GridBagConstraints gbc_btnClear = new GridBagConstraints();
		gbc_btnClear.anchor = GridBagConstraints.EAST;
		gbc_btnClear.insets = new Insets(0, 0, 5, 5);
		gbc_btnClear.gridx = 1;
		gbc_btnClear.gridy = 8;
		urlPanel.add(btnClear, gbc_btnClear);
	}

	public static void main(String[] args)
	throws Exception {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		new ASLibGui();
	}

}
